/*     */ package com.Ticker.provider;
/*     */ 
/*     */ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
/*     */ 
/*     */ public class YahooProvider
/*     */   implements Runnable
/*     */ {
/*     */   private Thread feedThread;
/*     */   private List<String> symbols;
/*  17 */   private List<UpdateListener> listeners = new ArrayList<UpdateListener>();
/*     */ 
/*     */   public YahooProvider() {
/*  20 */     this.symbols = new ArrayList<String>();
/*     */   }
/*     */ 
/*     */   public void addUpdateListener(UpdateListener listener) {
/*  24 */     synchronized (this.listeners) {
/*  25 */       if (!this.listeners.contains(listener))
/*  26 */         this.listeners.add(listener);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void removeUpdateListener(UpdateListener listener)
/*     */   {
/*  32 */     synchronized (this.listeners) {
/*  33 */       this.listeners.remove(listener);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void addSymbol(String symbol)
/*     */   {
/*  41 */     synchronized (this.symbols) {
/*  42 */       this.symbols.add(symbol);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void removeSymbol(String symbol)
/*     */   {
/*  51 */     synchronized (this.symbols) {
/*  52 */       this.symbols.remove(symbol);
/*     */     }
/*     */   }
/*     */ 
/*     */   public void removeAllElements()
/*     */   {
/*  60 */     synchronized (this.symbols) {
/*  61 */       this.symbols.clear();
/*     */     }
/*     */   }
/*     */ 
/*     */   private void readSymbolData(String symbol)
/*     */   {
/*  69 */     String feedURL = "http://finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=sl9p4&e=.csv";
/*  70 */     URL url = null;
/*     */     try {
/*  72 */       url = new URL(feedURL);
/*     */     } catch (MalformedURLException e1) {
/*  74 */       System.out.println("Unable to open connection !");
/*  75 */       return;
/*     */     }
/*  77 */     BufferedReader br = null;
/*     */     try {
/*  79 */       br = new BufferedReader(new InputStreamReader(url.openStream()));
/*     */     } catch (IOException e1) {
/*  81 */       e1.printStackTrace();
/*     */     }
/*  83 */     if (br == null) {
/*  84 */       System.out.println("Cannot read data from feed !");
/*  85 */       return;
/*     */     }
/*  87 */     String line = null;
/*     */     try
/*     */     {
/*  90 */       line = br.readLine();
/*     */     }
/*     */     catch (IOException localIOException1) {
/*     */     }
/*  94 */     if (line != null) {
/*  95 */       StringTokenizer st = new StringTokenizer(line, ",");
/*     */ 
/*  97 */       if (st.hasMoreTokens()) {
/*  98 */         String name = st.nextToken();
/*  99 */         if (name.startsWith("\"")) {
/* 100 */           name = name.substring(1, name.length() - 1);
/*     */         }
/* 102 */         if (!name.equals(symbol))
/* 103 */           return;
/*     */       }
/*     */       else {
/* 106 */         return;
/*     */       }
/*     */ 
/* 109 */       Double last = null;
/* 110 */       if (st.hasMoreTokens()) {
/* 111 */         last = parseDouble(st.nextToken());
/*     */       }
/*     */ 
/* 114 */       Double changePercent = null;
/* 115 */       if (st.hasMoreTokens()) {
/* 116 */         changePercent = parseDouble(st.nextToken());
/*     */ 
/* 120 */         if (Math.abs(changePercent.doubleValue()) > 50.0D) {
/* 121 */           changePercent = new Double((0.0D / 0.0D));
/*     */         }
/*     */       }
/*     */ 
/* 125 */       if ((last != null) && (changePercent != null)) {
/* 126 */         Stock stock = new Stock(symbol, last, changePercent);
/* 127 */         sendStock(stock);
/*     */       }
/*     */     }
/*     */     try {
/* 131 */       br.close();
/*     */     } catch (IOException e) {
/* 133 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   private void sendStock(Stock stock) {
/* 138 */     synchronized (this.listeners) {
/* 139 */       for (Iterator<UpdateListener> it = this.listeners.iterator(); it.hasNext(); )
/* 140 */         ((UpdateListener)it.next()).updateStock(stock);
/*     */     }
/*     */   }
/*     */ 
/*     */   private Double parseDouble(String s)
/*     */   {
/* 146 */     if (s.startsWith("\"")) {
/* 147 */       s = s.substring(1, s.length() - 1);
/*     */     }
/* 149 */     if (s.endsWith("%")) {
/* 150 */       s = s.substring(0, s.length() - 1);
/*     */     }
/*     */     try
/*     */     {
/* 154 */       return Double.valueOf(s); } catch (NumberFormatException e) {
/*     */     }
/* 156 */     return null;
/*     */   }
/*     */ 
/*     */   public void start()
/*     */   {
/* 161 */     this.feedThread = new Thread(this, "Yahoo Provider");
/* 162 */     this.feedThread.start();
/*     */   }
/*     */ 
/*     */   public void stop() {
/* 166 */     Thread t = this.feedThread;
/* 167 */     this.feedThread = null;
/* 168 */     if (t != null)
/* 169 */       t.interrupt();
/*     */   }
/*     */ 
/*     */   public void run()
/*     */   {
/* 174 */     Thread currentThread = Thread.currentThread();
/*     */ 
/* 176 */     int index = 0;
/*     */ 
/* 179 */     while (currentThread == this.feedThread) {
/* 180 */       long time = System.currentTimeMillis();
/*     */       String symbol;
/* 181 */       synchronized (this.symbols) {
/* 182 */         if (index >= this.symbols.size())
/* 183 */           index = 0;
/*     */         if (this.symbols.isEmpty())
/* 186 */           symbol = null;
/*     */         else
/* 188 */           symbol = (String)this.symbols.get(index++);
/*     */       }
/*     */       if (symbol != null) {
/* 192 */         readSymbolData(symbol);
/*     */       }
/* 194 */       time = 1000L - (System.currentTimeMillis() - time);
/* 195 */       if (time <= 10L) continue;
/*     */       try {
/* 197 */         Thread.sleep(time);
/*     */       }
/*     */       catch (InterruptedException localInterruptedException)
/*     */       {
/*     */       }
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\Users\os\Downloads\TrimOnline.jar
 * Qualified Name:     Ticker.provider.YahooProvider
 * JD-Core Version:    0.5.4
 */